define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ta/index' + location.search,
                    add_url: 'ta/add',
                    edit_url: 'ta/edit',
                    del_url: 'ta/del',
                    multi_url: 'ta/multi',
                    import_url: 'ta/import',
                    table: 'ho',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        // {field: 'cover', title: __('Cover'), operate: 'LIKE'},
                        {field: 'cover', title: __('Cover'), formatter: Controller.api.formatter.articleCover, events: Controller.api.events.previewVideo},
                        {field: 'type', title: __('Type'), formatter: Controller.api.formatter.type},
                        {field: 'author', title: __('Author'), operate: 'LIKE'},
                        {field: 'look_num', title: __('Look_num')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            // 配置某一些列显示的格式
            formatter: {
                type: function (value) {
                    switch (value) {
                        case 1:
                            return "<span class='label label-info'>文本</span>";
                        case 2:
                            return "<span class='label label-success'>视频</span>";
                    }
                },
                articleCover: function (value, row) {
                    if (row.type == 1) {
                        return "<button class='btn btn-info btn-xs preview-article-video'>预览内容</button>";
                    } else {
                        return "<button class='btn btn-success btn-xs preview-article-video'>预览视频</button>";
                    }
                }
            },
            // 配置事件
            events: {
                // 事件的名称
                previewVideo: {
                    'click button.preview-article-video': function (e, value, row) {
                        // 判断有没有视频
                        if (row.type == 1) {
                            layer.open({
                                type: 1,
                                title: '内容预览',
                                content: "<div style='padding: 20px;'>" + row.content + "</div>",
                                area: ['800px', '600px']
                            });
                        } else {
                            layer.open({
                                type: 2,
                                title: '视频预览',
                                content: value,
                                area: ['800px', '600px']
                            });
                        }
                    }
                }
            }
        }
    };
    return Controller;
});